/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.applicatioN.controllers;

import com.applicatioN.controllers.exceptions.NonexistentEntityException;
import com.applicatioN.controllers.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.applicatioN.entities.Agent;
import com.applicatioN.entities.Entreprise;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Asus_k53s
 */
public class EntrepriseJpaController implements Serializable {

    public EntrepriseJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Entreprise entreprise) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Agent numeroDeMatricule = entreprise.getNumeroDeMatricule();
            if (numeroDeMatricule != null) {
                numeroDeMatricule = em.getReference(numeroDeMatricule.getClass(), numeroDeMatricule.getNumeroDeMatricule());
                entreprise.setNumeroDeMatricule(numeroDeMatricule);
            }
            em.persist(entreprise);
            if (numeroDeMatricule != null) {
                numeroDeMatricule.getEntrepriseCollection().add(entreprise);
                numeroDeMatricule = em.merge(numeroDeMatricule);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findEntreprise(entreprise.getNomEse()) != null) {
                throw new PreexistingEntityException("Entreprise " + entreprise + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Entreprise entreprise) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Entreprise persistentEntreprise = em.find(Entreprise.class, entreprise.getNomEse());
            Agent numeroDeMatriculeOld = persistentEntreprise.getNumeroDeMatricule();
            Agent numeroDeMatriculeNew = entreprise.getNumeroDeMatricule();
            if (numeroDeMatriculeNew != null) {
                numeroDeMatriculeNew = em.getReference(numeroDeMatriculeNew.getClass(), numeroDeMatriculeNew.getNumeroDeMatricule());
                entreprise.setNumeroDeMatricule(numeroDeMatriculeNew);
            }
            entreprise = em.merge(entreprise);
            if (numeroDeMatriculeOld != null && !numeroDeMatriculeOld.equals(numeroDeMatriculeNew)) {
                numeroDeMatriculeOld.getEntrepriseCollection().remove(entreprise);
                numeroDeMatriculeOld = em.merge(numeroDeMatriculeOld);
            }
            if (numeroDeMatriculeNew != null && !numeroDeMatriculeNew.equals(numeroDeMatriculeOld)) {
                numeroDeMatriculeNew.getEntrepriseCollection().add(entreprise);
                numeroDeMatriculeNew = em.merge(numeroDeMatriculeNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = entreprise.getNomEse();
                if (findEntreprise(id) == null) {
                    throw new NonexistentEntityException("The entreprise with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Entreprise entreprise;
            try {
                entreprise = em.getReference(Entreprise.class, id);
                entreprise.getNomEse();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The entreprise with id " + id + " no longer exists.", enfe);
            }
            Agent numeroDeMatricule = entreprise.getNumeroDeMatricule();
            if (numeroDeMatricule != null) {
                numeroDeMatricule.getEntrepriseCollection().remove(entreprise);
                numeroDeMatricule = em.merge(numeroDeMatricule);
            }
            em.remove(entreprise);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Entreprise> findEntrepriseEntities() {
        return findEntrepriseEntities(true, -1, -1);
    }

    public List<Entreprise> findEntrepriseEntities(int maxResults, int firstResult) {
        return findEntrepriseEntities(false, maxResults, firstResult);
    }

    private List<Entreprise> findEntrepriseEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Entreprise.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Entreprise findEntreprise(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Entreprise.class, id);
        } finally {
            em.close();
        }
    }

    public int getEntrepriseCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Entreprise> rt = cq.from(Entreprise.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
